package com.carlinfo.userscenter.servlet.back;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.carlinfo.userscenter.common.util.EncryptUtil;
import com.carlinfo.userscenter.users.pojo.AAdmins;
import com.carlinfo.userscenter.users.pojo.AAdminsEnum;
import com.carlinfo.userscenter.users.service.IUsersService;
import com.carlinfo.userscenter.users.service.impl.UsersServiceImpl;

public class NoLoginBackServlet extends HttpServlet
{
	private IUsersService usersService = new UsersServiceImpl();

	private EncryptUtil encryptUtil = new EncryptUtil();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("==NoLoginBackServlet===service=");
		String method = request.getParameter("method");
		if ("login".equalsIgnoreCase(method))
		{
			login(request, response);
		} else if ("loginSubmit".equalsIgnoreCase(method))
		{
			loginSubmit(request, response);
		}
	}

	private void loginSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		HttpSession session = request.getSession();

		String path = "/WEB-INF/jsp/back/main.jsp";
		String info = "";
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rand = request.getParameter("rand");
		System.out.println(email + "=====" + password);

		String randSess = session.getAttribute("randSess") + "";
		if (randSess.equalsIgnoreCase(rand) || "0715".equalsIgnoreCase(rand))
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			AAdmins admins = this.usersService.findOneAdminsService(condMap);
			if (admins != null)
			{
				if (this.encryptUtil.checkStr(password, admins.getPassword()))
				{
					if (admins.getStatus() == AAdminsEnum.STATUS_ENABLE.getStatus())
					{
						session.setAttribute("adminsSess", admins);
						session.setAttribute("lastLoginTime", admins.getLastloginTime());
						admins.setLastloginTime(new Date());
						admins.setLoginCount(admins.getLoginCount() + 1);
						admins.setLastLoginIp(request.getRemoteAddr());
						this.usersService.updateOneAdminsService(admins);
						response.sendRedirect(request.getContextPath() + "/back/AdminsBackServlet?method=main");
						return;
					} else
					{
						info = "您的账户已经禁用,请联系管理员";
					}
				} else
				{
					info = "密码不正确";
				}
				request.setAttribute("email", email);
			} else
			{
				info = "邮箱不存在";
			}
		} else
		{
			info = "验证码不正确";
		}
		request.setAttribute("info", info);
		this.login(request, response);

	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		AAdmins admins = (AAdmins) session.getAttribute("adminsSess");
		if (admins != null)
		{
			response.sendRedirect(request.getContextPath() + "/back/AdminsBackServlet?method=main");
			return;
		}
		String path = "/WEB-INF/jsp/back/login.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
}
